require('dotenv').config();
const search = require('./utils/search')
const config = require('config')
import {addUserGroup} from './utils/nc/user'
const group = config.get('nextcloud.group')

const init = async ()=>{
    try{
        await addUserGroup(group)
    }catch(err){
        if(err?.error?.ocs?.meta?.statuscode==102){
            console.log(`user group ${group} in nextcloud already created`)
        }else{
            console.error(err)
        }
    }
    await search.init(process.env['ES_FORCE_REBUILD']=='true')
}

if (require.main === module) {
    init().then(() => {
            console.log('cmap initialize all success');
            process.exit(0);
        }
    ).catch((err) => {
        console.log(err.stack || err)
    })
}
